//
//  HQEmployeeManageViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/6/19.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQEmployeeManageViewController.h"

@interface HQEmployeeManageViewController ()

@end

@implementation HQEmployeeManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"零售商管理";

    [self requestList];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HQEmployeeManageCell *cell = [HQEmployeeManageCell cellWithTableView:tableView];
    cell.dataInfo = _dataSource[indexPath.row];
    [cell.deleteItem addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

/**
 删除关系

 @param button 按钮
 */
-(void)deleteItem:(HQButton *)button{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam deletshop:[UserModelManager userID] other_id:button.dataInfo[@"other_id"]] withMethod:[HQMethod deletshop] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            [self tostView:@"解除合作关系成功"];
            [self requestList];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 获取合作列表
 */
-(void)requestList{
    _tableView.backgroundView = nil;
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam listshop:[UserModelManager userID]] withMethod:[HQMethod listshop] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
           _dataSource = [[NSMutableArray alloc]initWithArray:sucData[@"data"]];
            if(_dataSource.count ==0){
                [self addNoneDataView:_tableView withText:@"暂无数据"];
            }
           [_tableView reloadData];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 添加合作关系

 @param sender 按钮
 */
- (IBAction)addShopItem:(id)sender {
    if([self isInputEmpty:_phoneNo.text]){
        [self tostView:@"请输入手机号"];
        return;
    }
    if(![HQTool phoneNoRegex:_phoneNo.text]){
        [self tostView:@"请输入正确的手机号"];
        return;
    }
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam addshop:[UserModelManager userID] phone:_phoneNo.text] withMethod:[HQMethod addshop] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            [self tostView:@"添加合作关系成功"];
            [self requestList];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

@end
